package com.zxn.xiaoran.view.activity;

import android.annotation.SuppressLint;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.zcommon.lib.SystemIntent;
import com.zxn.presenter.presenter.CreatePresenter;
import com.zxn.xiaoran.R;
import com.zxn.xiaoran.presenter.AppViewPresenter;
import com.zxn.xiaoran.view.CommonDgFrag;
import com.zxn.xiaoran.view.base.XMActivity;
import com.zxn.xiaoran.view.frag.SlideFragment;

import java.util.ArrayList;
import java.util.List;

@CreatePresenter(AppViewPresenter.class)
public class HomeActivity extends XMActivity<AppViewPresenter> implements AppViewPresenter.IAppView {

    public static void jumpTo(Context context) {
        Intent intent = new Intent(context, HomeActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_home);
//        LoveSurfaceView surfaceView = new LoveSurfaceView(this);
//        setContentView(surfaceView);
//        Toolbar mToolbar = findViewById(R.id.tool_bar);
//        setSupportActionBar(mToolbar);

        mFragmentManager = getSupportFragmentManager();
        mPresenter.appCheck();

        initFragments();
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_home;
//        return 0;
    }


    @Override
    public void onAppCheck() {
        if (mPresenter.hasNewVersion()) {
            showDg();
        }
    }

    private void showDg() {
        CommonDgFrag.newInstance(new CommonDgFrag.IDialogCreate() {
            @Override
            public void onConfirmClick(View view) {
                SystemIntent.jumpToBrowser(HomeActivity.this, mPresenter.getCheckInfo().data.downloadURL);
            }

            @Override
            public String getTitle() {
                return "温馨提示";
            }

            @Override
            public String getContent() {
                return "是否进行版本升级";
            }

            @Override
            public boolean isSingleButton() {
                return false;
            }

        }).show(getSupportFragmentManager());
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.item_0:
                switchFragment(0);
                break;
            case R.id.item_1:
                //switchFragment(1);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }
    private FragmentManager mFragmentManager;
    private List<Fragment> mFragments = new ArrayList<>();//存储所有的Fragment对象
    private Fragment mCurrentFragment;
    @SuppressLint("WrongConstant")
    private void switchFragment(int position) {
        mFragmentManager.beginTransaction()
                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
                .hide(mCurrentFragment)
                .show(mFragments.get(position))
                .commit();
        mCurrentFragment = mFragments.get(position);
        //mTvTitle.setText(mManagerNames.get(position));
    }

    private void initFragments() {
//        EchelonFragment echelonFragment = new EchelonFragment();//梯形布局
//        mFragments.add(echelonFragment);
//        mManagerNames.add("EchelonLayoutManager");
//
//        PickerFragment pickerFragment = new PickerFragment();//选择器布局
//        mFragments.add(pickerFragment);
//        mManagerNames.add("PickerLayoutManager");

        SlideFragment slideFragment = new SlideFragment();//滑动布局
        mFragments.add(slideFragment);
//        mManagerNames.add("SlideLayoutManager");

        mFragmentManager.beginTransaction()
                .add(R.id.container_layout, mFragments.get(0))
//                .add(R.id.container_layout,mFragments.get(1))
//                .add(R.id.container_layout,mFragments.get(2))
//                .hide(mFragments.get(2))
//                .hide(mFragments.get(1))
                .show(mFragments.get(0))
                .commit();
        mCurrentFragment = mFragments.get(0);
        //mTvTitle.setText(mManagerNames.get(0));
    }

}
